package com.jiuyou.clientController.mall.product;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jiuyou.clientController.base.ClientBaseController;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.product.domain.StoreProductBack;
import com.ruoyi.shopping.domain.MallClassify;
import com.ruoyi.shopping.domain.MallProduct;
import com.ruoyi.shopping.domain.MallProductSpecs;
import com.ruoyi.shopping.service.IMallClassifyService;
import com.ruoyi.shopping.service.IMallProductService;
import com.ruoyi.shopping.service.IMallProductSpecsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.Oneway;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chengliang
 * @date 2022/6/16
 **/
@RestController
@RequestMapping("/client/mall/product")
public class MallProductClientContorller extends ClientBaseController {
    @Autowired
    private IMallProductService productService;

    @Autowired
    private IMallClassifyService classifyService;
    @Autowired
    private IMallProductSpecsService specsService;
    @PostMapping("/getHot")
    public TableDataInfo getHot(){
        MallProduct mallProduct = new MallProduct();
        mallProduct.setWhetherSelling(DicConstants.SYS_Y.getCode());
        mallProduct.setIsShow(DicConstants.SYS_Y.getCode());
        startPage();
        List<MallProduct> list = productService.selectMallProductList(mallProduct);
        TableDataInfo dataTable = getDataTable(list);
        return dataTable;
    }
    @PostMapping(value = "/checkCan/{id}")
    public boolean checkCan(@PathVariable("id") Long id)
    {
        MallProduct product = productService.selectMallProductById(id);
        if(null==product || DicConstants.SYS_N.equals(product.getIsShow()) || DicConstants.SYS_Y.getCode().equals(product.getDelState())){
            return false;
        }
        return true;
    }
    @PostMapping("/getList")
    public TableDataInfo getList(Long firstClassifyId,Long secondClassifyId,String searchValue){
        List<MallProduct> result = new ArrayList<MallProduct>();

        if(!StringUtils.isEmpty(searchValue)){
            //有搜索则只考虑搜索条件
            startPage();
            MallProduct mallProduct = new MallProduct();
            mallProduct.getParams().put("searchValue", SqlUtil.participleStr(searchValue));
            result = productService.selectMallProductList(mallProduct);
        }else {
            if(secondClassifyId == -1){
                MallClassify mallClassify = new MallClassify();
                mallClassify.setParentId(firstClassifyId);
                List<MallClassify> mallClassifies = classifyService.selectMallClassifyList(mallClassify);
                ArrayList<Long> ids = new ArrayList<>();
                mallClassifies.forEach(e->{
                    ids.add(e.getId());
                });
                List<Long> second = ids.stream().distinct().collect(Collectors.toList());
                Long[] sIds = second.toArray(new Long[second.size()]);
                startPage();
                result = productService.selectSecondClassifyProductList(sIds);
            }else{
                MallProduct mallProduct = new MallProduct();
                mallProduct.setClassifyId(secondClassifyId);
                mallProduct.setIsShow(DicConstants.SYS_Y.getCode());
                startPage();
                result = productService.selectMallProductList(mallProduct);
            }
        }

        TableDataInfo dataTable = getDataTable(result);
        return dataTable;
    }
    //商品相情
    @PostMapping("/getDetail")
    public AjaxResult getDetail(Long productId){
        JSONObject jso = new JSONObject();
        MallProduct product = productService.selectMallProductById(productId);
//        商品信息
        if(product == null){
            return AjaxResult.error();
        }
        jso.put("productName",product.getProductName());
        jso.put("currentPrice",product.getCurrentPrice());
        jso.put("oldPrice",product.getOldPrice());
        jso.put("salesNum",product.getSalesNum());
        jso.put("video",product.getProductMainVideo());
        jso.put("videoCover",product.getVideoCover());
        jso.put("mainImg",product.getProductImage());
        jso.put("secondaryImg",product.getProductDeputyImage());
        jso.put("classifyId",product.getClassifyId());
        String richText=product.getRichText();
        jso.put("richText",richText);
        MallProductSpecs specs = new MallProductSpecs();
        specs.setProductId(product.getId());
        //规格列表
        List<MallProductSpecs> specsList = specsService.selectMallProductSpecsList(specs);
        TableDataInfo dataTable = getDataTable(specsList);
        jso.put("specs",specsList);
        jso.put("specsNum",specsList.size());

        return AjaxResult.success(jso);
    }
}
